<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>图书管理</el-breadcrumb-item>
      <el-breadcrumb-item>图书列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入要查询的书名或作者"
            v-model="book_name"
            clearable
            @clear="getBookListByName"
            @keyup.enter.native="getBookListByName"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getBookListByName"
            ></el-button></el-input
        ></el-col>
      </el-row>
      <el-table :data="bookList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="image" label="封面">
          <template slot-scope="scope">
            <el-popover placement="top-start" title="" trigger="hover">
              <img
                :src="scope.row.image"
                alt=""
                style="width: 150px; height: 150px"
              />
              <img
                slot="reference"
                :src="scope.row.image"
                style="width: 50px; height: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="book_name" label="书名"></el-table-column>
        <el-table-column prop="author" label="作者"></el-table-column>
        <!-- <el-table-column prop="details" label="简介"></el-table-column> -->
        <el-table-column prop="enable" label="借阅状态"></el-table-column>
        <el-table-column prop="owner" label="发布人"></el-table-column>
        <el-table-column prop="phone" label="联系电话"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="removeBookById(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bookList: [],
      book_name: "",
    };
  },
  created() {
    this.getBooksList();
  },
  methods: {
    async getBooksList() {
      let res = await this.$http.get("book");
      if (res.data.status !== 200) {
        return this.$message.error(res.data.message);
      }
      console.log(res.data.bookList);
      this.bookList = res.data.bookList.map((item) => {
        let enable = item.enable ? "可借阅" : "借阅中";
        return { ...item, enable };
      });
      console.log(this.bookList);
      // this.$message.success(res.data.message);
    },
    //搜索功能
    async getBookListByName() {
      let keyValue = this.book_name;
      let res = await this.$http.post("book/search", {
        keyValue,
      });
      if (res.data.status !== 200) {
        return this.$message.error(res.data.message);
      }
      this.bookList = res.data.bookList.map((item) => {
        let enable = item.enable ? "可借阅" : "借阅中";
        return { ...item, enable };
      });
      console.log(this.bookList);
      this.$message.success(res.data.message);
    },
    // 删除书籍
    async removeBookById(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该书籍, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((error) => error);
      if (confirmResult !== "confirm") {
        return this.$message.info("取消了删除！");
      }
      let res = await this.$http.delete(`book/${id}`);
      if (res.data.status !== 200) {
        return this.$message.error(res.data.message);
      }
      this.getBooksList();
      this.$message.success(res.data.message);
    },
  },
};
</script>

<style lang="less" scope>
.el-tag {
  margin-top: 15px;
}
.bt {
  border-top: 1px solid #eee;
}
.bb {
  border-bottom: 1px solid #eee;
}
.vercenter {
  display: flex;
  align-items: center;
}
</style>